"use strict";
var initDb = require("./untils/initDb");
var Crawler = require("crawler");
var article = require('./model/book');
//initDb();
/*var seq=require("./db");
seq
  .authenticate()
  .then(() => {
    console.log('Connection has been established successfully.');
  })
  .catch(err => {
    console.error('Unable to connect to the database:', err);
  });
*/
for (let index = 0; index < 200; index +=1) {
 
    var URL = 'https://www.cnblogs.com/sitehome/p/' + index;
    var c = new Crawler({
      rateLimit:2000,
      maxConnections : 1,
      //maxConnections: 10,
      // This will be called for each crawled page
      callback: function (error, res, done) {
        if (error) {
          console.log(error);
        } else {
          var $ = res.$;
          var list = $('#main #post_list .post_item .post_item_body').toArray();
          list.forEach(element => {
            var titel = $(element).find('h3 a').text()//.trim().replace(/[ \n\r]/g, '');
            var autlor = $(element).find('.post_item_foot .lightblue').text()//.trim().replace(/[ \n\r]/g, '');
            var Time =$(element).find('.post_item_foot').text().match(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}/).toString();
            console.log(Time);
            article.create({titel:titel,autlor:autlor,Time:Time})
            //var name=__dirname+'/img/'+img.slice(img.lastIndexOf('/')+1);
            
            //console.log(txt);
  
            //console.log(txt,autlor,Introedoution);
           // console.log(imgs);
            
            
          });
          
          
          // $ is Cheerio by default
          //a lean implementation of core jQuery designed specifically for the server
          //console.log($("title").text());
        }
        done();
      }
    });
  
  
    c.queue(URL);
  }